

const Message = (msg, delayTime = 4) => {
  createMessageDom(msg, delayTime)
  let timer = setTimeout(() => {
    close()
    clearTimeout(timer)
  }, delayTime * 1000)
}

// create Document
function createMessageDom ( msg, delayTime) {
  let msgWrap = document.createElement('div')
  msgWrap.setAttribute('class', 'hj-message')
  msgWrap.style.animationDuration = delayTime + 's'
  msgWrap.innerHTML = msg
  document.body.appendChild(msgWrap)
}

// close Document
const close = function () {
  let randerDom = document.getElementsByClassName('hj-message')
  if (randerDom.length > 0) {
    document.body.removeChild(randerDom[0])
  }
}

Message.close = close

export { Message }